package com.leetcodehot.problems;

public class problems1448 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    private int ans = 0;

    public int goodNodes(TreeNode root) {
        dfs(root, -1000000);
        return ans;
    }

    private void dfs(TreeNode root, int preMax) {
        if (root == null) return;
        if (root.val >= preMax) {
            ans++;
            preMax = root.val;
        }
        dfs(root.left, preMax);
        dfs(root.right, preMax);
    }
}
